package com.example.bookstore.api

import com.example.bookstore.dao.BookDao
import com.example.bookstore.entity.BookEntity
import com.example.bookstore.service.BookService
import com.example.bookstore.validate.PageNSizeValidate
import io.swagger.annotations.Api
import io.swagger.annotations.ApiImplicitParam
import io.swagger.annotations.ApiImplicitParams
import io.swagger.annotations.ApiOperation
import org.hibernate.validator.constraints.Range
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import javax.validation.constraints.NotBlank

/**
 * Created by IntelliJ IDEA.
 * Book: lvshe
 * Date: 2019/8/10
 * Time: 15:14
 */
@Api(tags = ["书籍"], basePath = "/api/book")
@RestController
@RequestMapping("/api/book")
class BookController : BaseController<BookService, BookDao, BookEntity>() {

    @ApiOperation("猜你喜欢", notes = "获取猜你喜欢的图书列表")
    @ApiImplicitParams(ApiImplicitParam("猜你喜欢获取书籍的数目，默认为3，范围（0-100）", name = "number", allowableValues = "range(0, 100)", required = false))
    @GetMapping("/guessYouLike")
    fun guessYouLike(@Range(min = 0L, max = 100L) @RequestParam("number", defaultValue = "3", required = false) number: Int): Set<BookEntity> {
        return service.getGuessYouLikeBook(number)
    }

    @ApiOperation("获取书籍列表", notes = "获取书籍列表")
    @GetMapping("/list")
    override fun getAllPageable(@Validated pageNSizeValidate: PageNSizeValidate): MutableList<BookEntity> {
        return super.getAllPageable(pageNSizeValidate)
    }

    @ApiOperation("获取指定ID书籍列表", notes = "获取指定ID书籍列表")
    @ApiImplicitParams(ApiImplicitParam("书籍ID", name = "id", required = true))
    @GetMapping("/{id}")
    override fun getById(@Validated @NotBlank(message = "id 不能为空") @PathVariable id: String?): BookEntity {
        return super.getById(id)
    }
}